package com.acme.bankapp.domain.bank;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

public class Client implements Serializable {
    public static enum Gender {
        MALE("Mr. "), FEMALE("Ms. ");

        private String salutation;

        Gender(String salutation) {
            this.salutation = salutation;
        }

        @Override
        public String toString() {
            return salutation;
        }
    }

    private List<Account> accounts = new LinkedList<>();
    private String name;
    private Gender gender;
    private String emailAddress;

    public Client(Account account, Gender gender, String name, String emailAddress) {
        this(account, gender, name);
        this.emailAddress = emailAddress;
    }

    public Client(Account account, Gender gender, String name) {
        accounts.add(account);
        this.gender = gender;
        this.name = name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setGender(Gender gender) {
        this.gender = gender;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    public Gender getGender() {
        return gender;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public Account[] getAccounts() {
        return accounts.toArray(new Account[accounts.size()]);
    }

    public String getSalutation() {
        return gender + name;
    }

    public String getName() {
        return name;
    }

    public void addAdditionalAccount(Account account) {
        accounts.add(account);
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder("Client{accounts=");
        for (Account account : accounts) {
            result.append(account);
            result.append(", ");
        }
        result.append("name='");
        result.append(name);
        result.append("\', gender=");
        result.append(gender);
        result.append('}');

        return result.toString();
    }

    @Override
    public int hashCode() {
        int result = accounts != null ? accounts.hashCode() : 0;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (gender != null ? gender.hashCode() : 0);
        return result;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Client client = (Client) o;

        if (accounts != null ? !accounts.equals(client.accounts) : client.accounts != null) return false;
        if (gender != client.gender) return false;
        if (name != null ? !name.equals(client.name) : client.name != null) return false;

        return true;
    }
}
